#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    try:
       btn = WaitforElementById(driver,'com.nduoa.nmarket:id/me',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()
    #点击下载管理
    try:
        WaitforElementById(driver,'com.nduoa.nmarket:id/sns_downloadmgr',30).click()
    except NoSuchElementException:
        raise NoSuchElementException

    while True:
        try:
            tasks=WaitforElementsById(driver,"com.nduoa.nmarket:id/expandable_toggle_button",30)
        except NoSuchElementException:
            break
        else:
            tasks[0].click()
            try:
                clear_btn=WaitforElementById(driver,'com.nduoa.nmarket:id/app_delete',30)
            except NoSuchElementException:
                continue
            else:
                clear_btn.click()
            time.sleep(2)
    driver.back()
    try:
       btn = WaitforElementById(driver,'com.nduoa.nmarket:id/home',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

def clickappdown(driver):
    try:
        btns=WaitforElementsById(driver,'com.nduoa.nmarket:id/item_main',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        for btn in btns:
            try:
                size=WaitforElementById(btn,"com.nduoa.nmarket:id/category_text_desc_single",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size.split("|")[1].strip()
            if 'G' not in size:
                if 'K' in size or float(size.split("|")[1].strip()[0:-3])<10:
                    continue
            btn.click()
            try:
                down_btn=WaitforElementById(driver,'com.nduoa.nmarket:id/button_state',30)
            except NoSuchElementException:
                driver.back()
                continue
            else:
                down_btn.click()
                waittime=20
                while waittime:
                    text=down_btn.get_attribute("text")
                    print text
                    if '0.0%' not in text:
                        down_btn.click()
                        try:
                            delet_btn=WaitforElementById(driver,'com.nduoa.nmarket:id/image_likeOrCancel',30)
                        except NoSuchElementException:
                            raise NoSuchElementException
                        else:
                            delet_btn.click()
                        break
                    else:
                        time.sleep(0.5)
                        if waittime==0:
                            btn.click()
                            try:
                                delet_btn=WaitforElementById(driver,'com.nduoa.nmarket:id/image_likeOrCancel',30)
                            except NoSuchElementException:
                                raise NoSuchElementException
                            else:
                                delet_btn.click()
                            break
                        waittime=waittime-1
                print "----------------------------------------------"
                driver.back()
    #driver.back()



def soft(driver):
    categorys = [u'金融理财',u'旅游出行',u'拍摄美化',u'社交通讯',u'生活服务',u'实用工具',u'手机管理',u'手机美化',u'网上购物',u'学习办公',u'医疗健康',u'影音播放',u'资讯阅读']
    try:
        WaitforElementById(driver,'com.nduoa.nmarket:id/software',30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                WaitforElementByName(driver,u'全部',30).click()
            except NoSuchElementException:
                driver.back()
                continue
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def game(driver):
    categorys = [u'单机游戏',u'动作射击',u'角色扮演',u'经营策略',u'模拟辅助',u'棋牌桌游',u'体育竞速',u'网络游戏',u'休闲益智']
    try:
        WaitforElementById(driver,'com.nduoa.nmarket:id/game',30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                WaitforElementByName(driver,u'全部',30).click()
            except NoSuchElementException:
                driver.back()
                continue
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION              # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.nduoa.nmarket'    # apk的包名
    desired_caps['appActivity'] = 'com.foresight.android.moboplay.main.MainActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    #del_appdown_task(driver)
    soft(driver)
    time.sleep(2)
    game(driver)
    driver.quit()

#run()